<?php

namespace app\controllers;


use app\common\base\BaseController;
use app\models\TbArticle;
use app\models\TbPublic;
use app\models\TbUser;
use Exception;
use Yii;
use yii\web\Cookie;


class SiteController extends BaseController
{

    public function init()
    {
        if (!Yii::$app->session->isActive) {
            Yii::$app->session->open();
        }
    }
    /**
     * 加载新闻页
     * @return string
     */
    public function actionNews(){
        $countries =TbPublic::selecttopfives();
        $topten=TbUser::ToptenCelebrity();
        $publicall=TbPublic::selectAll();
        Yii::trace($publicall);
        return $this->render('news',[
            'countries'=>$countries,
            'publicall'=>$publicall,
            'r'=>$topten
        ]);
    }
    /**
     * 加载主页
     * @return string
     */
    public function actionIndex(){
        $countries =TbPublic::selecttopfives();
        $topten=TbUser::ToptenCelebrity();
        list($articleall,$pagination)=TbArticle::selectAll();
        $view =Yii::$app->view;
        $view->params['menu']="menumenu";
           return $this->render('index',[
            'countries'=>$countries,
            'articleall'=>$articleall,
            'pagination'=>$pagination,
            'r'=>$topten
        ]);
    }

    /**
     * 加载登录页
     * @return string
     */
    public function actionLogin(){
        return $this->render('login');
    }

    /**
     * 加载注册页
     * @return string
     */
    public function actionRegistered(){

        return $this->render("registered");
    }

    /**
     * @return \yii\web\Response 退出操作
     */
    public function actionLogout(){
        Yii::$app->session->destroy();
        return $this->redirect(['site/index']);
    }

    /**
     * 注册页提交操作方法
     * @return string
     */
    public function actionRegisteredSubmit()
    {
        try{
            $tbuser = new TbUser();
            $username = $this->getParam('username');
            $password = $this->getParam('password');
            $remember = $this->getParam('remember');
            $selectRegname = $tbuser->selectRegname($username);

            if ($selectRegname != null) {
                return $this->out(false, '用户名已存在');
            }
            $hashpwd=Yii::$app->getSecurity()->generatePasswordHash($password);
            Yii::trace($hashpwd);
            $tbuser->regname = $username;
            $tbuser->regpwd = $hashpwd;
            if ($tbuser->save()){
                if ($remember=="1") {
                    $cookies = \Yii::$app->response->cookies;
                    $cookies->add(new Cookie([
                        'name' => 'regname',
                        'value' => $username,
                        'expire' => time() + 7 * 24 * 60 * 60,
                    ]));
                }
                Yii::$app->session->set('regname',$username);
                return $this->out(true, '注册成功');
            }else {
                return $this->out(false, '注册失败');
            }
        }catch (Exception $e){
            return $this->out(false, '注册失败');
        }
    }

    /**
     * 登录页提交操作方法
     * @return string
     */
    public function actionLoginSubmit()  {
        try {
            $username = $this->getParam('username');
            $password = $this->getParam('password');
            $remember = $this->getParam('remember');
            $login = TbUser::Login($username);
            if ($login == null){
                return $this->out(false, '账号不存在');
            }
            if(!Yii::$app->getSecurity()->validatePassword($password, $login['regpwd'])) {
                return $this->out(false, '密码不正确');
            }
            if ($remember=="1"){
                $cookies=\Yii::$app->response->cookies;
                $cookies->add(new Cookie([
                    'name'=>'regname',
                    'value'=> $login['regname'],
                    'expire' => time()+7*24*60*60,
                ]));
            }
            \Yii::$app->session->set('regname',$login['regname']);
            return $this->out(true, '登录成功');
        } catch (Exception $e) {
            return $this->out(false, '登录失败');
        }
    }

    /**
     * 跳转到新闻详情页
     * @return string
     */
    public function actionNewsDetailed($id){
        $public = TbPublic::selectId($id);
        $countries =TbPublic::selecttopfives();
        $topten=TbUser::ToptenCelebrity();
        return $this->render('newsdetailed',[
            'public'=>$public,
            'countries'=>$countries,
            'r'=>$topten
        ]);
    }
}
